package com.jlkf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.RoleMapper;
import com.jlkf.service.RoleService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.MD5Util;
import com.platform.server.util.StringTools;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * 角色列表
	 */
	@ResponseBody
	@RequestMapping("/roleList")
	public Object roleList(Integer pageNumber,Integer pageSize,String name,String type) {
		if("1".equals(type)){
			List<Map<String,Object>> lists = roleService.roleList(new HashMap<>());
			return new ReturnData(lists,200,"角色列表");
		}
		Map<String, Object> map = new HashMap<String, Object>();
	    if(!StringTools.isEmpty(name)){
	    	map.put("name", name);
	    }
	    PageHelper.startPage(pageNumber, pageSize);
		List<Map<String,Object>> lists = roleService.roleList(map);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"角色列表");
	}
	
	/**
	 * 添加角色
	 */
	@ResponseBody
	@RequestMapping("/addRole")
	public Object addRole(@RequestBody Map<String,Object> map) {
		String content = (String)map.get("content");
		String[] split = content.split(";");
		String roleId = "";
		String remark = "";
		String msg = "";
		for (String string : split) {
			String[] split2 = string.split(",");
			for (String string2 : split2) {
				if(!(split2[1]).equals(msg)) {
					remark += split2[1] + ",";
					msg = split2[1];
				}else {
					roleId += split2[0] + ",";
					break;
				}
			}
		}
		map.put("source_id", roleId);
		map.put("remark", remark);
		int flag = roleService.addRole(map);
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(200,"添加失败");
		}
	}
	
	/**
	 * 删除角色
	 */
	@ResponseBody
	@RequestMapping("/deletedRole")
	public Object deletedRole(Integer id) {
		//查看角色是否已被分配
		int isUse = roleMapper.selectRoleUse(id);
		if(isUse > 0){
			return new ReturnData(12,"该角色已被使用，无法删除");
		}
		int flag = roleService.deletedRole(id);
		if(flag > 0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(100,"删除失败");
		}
	}
	
	/**
	 * 角色详情
	 */
	@ResponseBody
	@RequestMapping("/roleDetail")
	public Object roleDetail(Integer id) {
		return new ReturnData(roleService.roleDetail(id),200,"成功");
	}
	
	/**
	 * 编辑角色
	 */
	@RequestMapping("/updateRole")
	@ResponseBody
	public Object updateRole(@RequestBody Map<String,Object> map) {
		String content = (String)map.get("content");
		String[] split = content.split(";");
		String roleId = "";
		String remark = "";
		String msg = "";
		for (String string : split) {
			String[] split2 = string.split(",");
			for (String string2 : split2) {
				if(!(split2[1]).equals(msg)) {
					remark += split2[1] + ",";
					msg = split2[1];
				}else {
					roleId += split2[0] + ",";
					break;
				}
			}
		}
		map.put("source_id", roleId);
		map.put("remark", remark);
		int flag = roleService.updateRole(map);
		if(flag > 0){
			return new ReturnData(200,"编辑成功");
		}else{
			return new ReturnData(200,"编辑失败");
		}
	}
	
	/**
	 * 用户列表
	 */
	@ResponseBody
	@RequestMapping("/userList")
	public Object userList(Integer pageNumber,Integer pageSize,Integer status,String username,Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
	    if(status != null){
	    	map.put("status", status);
	    }
	    if(!StringTools.isEmpty(username)){
	    	map.put("username", username);
	    }
	    if(id != null){
	    	ModelAndView modelAndView = new ModelAndView();
	    	map.put("id", id);
	    	List<Map<String,Object>> lists = roleService.userList(map);
	    	modelAndView.addObject("result", lists.get(0));
	    	modelAndView.setViewName("jsp/roleManager/updateUser");
	    	return modelAndView;
	    }
	    PageHelper.startPage(pageNumber, pageSize);
		List<Map<String,Object>> lists = roleService.userList(map);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"用户列表");
	}
	
	/**
	 * 删除角色
	 */
	@ResponseBody
	@RequestMapping("/deleteSysUser")
	public Object deleteSysUser(Integer id) {
		int flag = roleService.deleteSysUser(id);
		if(flag > 0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(100,"删除失败");
		}
	}
	
	/**
	 * 重置密码
	 */
	@ResponseBody
	@RequestMapping("/retirePassword")
	public Object retirePassword(String id) throws Exception {
		String password = MD5Util.getMD5("123456");
		int flag = roleService.retirePassword(id,password);
		if(flag > 0){
			return new ReturnData(200,"重置成功");
		}else{
			return new ReturnData(100,"重置失败");
		}
	}
	
	/**
	 * 添加系统用户
	 */
	@ResponseBody
	@RequestMapping("/addSysUser")
	public Object addSysUser(@RequestBody Map<String,Object> map){
		try {
			map.put("password", MD5Util.getMD5(map.get("password").toString()));
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
		int flag = roleService.addSysUser(map);
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(100,"添加失败");
		}
	}
	/**
	 * 编辑系统用户
	 */
	@ResponseBody
	@RequestMapping("/updateSysUser")
	public Object updateSysUser(@RequestBody Map<String,Object> map) {
		int flag = roleService.updateSysUser(map);
		if(flag > 0){
			return new ReturnData(200,"编辑成功");
		}else{
			return new ReturnData(100,"编辑失败");
		}
	}
	
	/**
	 * 登录
	 */
	@ResponseBody
	@RequestMapping(value="/loginUser")
	public Object loginUser(@RequestBody Map<String,Object> map,HttpServletRequest request){
		try {
			return roleService.loginUser(map, request);
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	/**
	 * 退出
	 */
	@ResponseBody
	@RequestMapping("/loginout")
	public void loginout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		session.removeAttribute("sysUser");
		try {
			response.getWriter().write("<script>window.top.location.href=\"../login.jsp\";</script>");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
